package com.zq.service.impl;

import com.zq.entity.Tag;
import com.zq.entity.Type;
import com.zq.repository.TagRepository;
import com.zq.service.TagService;
import com.zq.utils.FenyeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.List;

/**
 * @Author:任人渐疏
 * @Date:Created in 12:10 下午 2020/6/27
 * Update:true
 */
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagRepository tagRepository;
    @Autowired
    private  TagService tagService;
    @Override
    public FenyeUtil<Tag> findAllList(Tag tag, Integer page) {
        FenyeUtil<Tag>  f=new FenyeUtil<>();
        PageRequest of = PageRequest.of(page - 1, 10);
        Page<Tag> all = tagRepository.findAll(get(tag), of);
        f.setCount((int) all.getTotalElements());
        f.setData(all.getContent());
        f.setCode(0);
        f.setMsg("");
        return f;
    }

    private Specification<Tag> get(Tag tag) {
    return  new Specification<Tag>() {
        @Override
        public Predicate toPredicate(Root<Tag> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
            Predicate conjunction = criteriaBuilder.conjunction();
            List<Expression<Boolean>> expressions = conjunction.getExpressions();
            if(!StringUtils.isEmpty(tag.getTagname())){
                expressions.add(criteriaBuilder.like(root.get("tagname"),"%"+tag.getTagname()+"%"));
            }
            return conjunction;
        }
    };
    }

    @Override
    public String addTag(Tag tag) {
        Tag byTagName = tagRepository.findByTagName(tag.getTagname());
        if(!StringUtils.isEmpty(byTagName)){
            return "已存在，请更换名称进行添加";
        }else{
            try {

                tagRepository.save(tag);
                return "添加成功";
            }catch (Exception e){
                return "添加失败";
            }
        }

    }

    @Override
    public String updateTag(Tag tag) {
        Tag byTagName = tagRepository.findByTagName(tag.getTagname());
        if (!StringUtils.isEmpty(byTagName)) {
            return "已存在，请更换名称进行修改";
        } else {
            try {
                tagRepository.saveAndFlush(tag);
                return "修改成功";
            } catch (Exception e) {
                return "修改失败";
            }
        }
    }
    @Override
    public String deleteTag(String tagid) {
        try {
            String[] split = tagid.split(",");
            for (int i = 0; i < split.length; i++) {
                Integer id= Integer.parseInt(split[i]);
                tagRepository.deleteById(id);
            }

            return "删除成功";
        }catch (Exception e){
            return "删除失败";
        }
    }
}
